﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HA.ModFlow.Domain.FileLayer;

namespace HA.ModFlow.Domain.DomainLayer
{

    /// <summary>
    /// 对于evt的抽象，只有第一层网格才应该考虑EVT问题
    /// </summary>
    [Serializable]
   public class EVTCollection:BoundaryCollecionBase
    {
        public EVTCollection(int nrows,int ncolumns,int nlayers,int nPeriods):base(nrows,ncolumns,nlayers)
        {
            this._evt= new Evapotranspiration(nrows, ncolumns, nlayers, nPeriods);
        }


        Evapotranspiration _evt;

        public Evapotranspiration EVT
        {
            get { return _evt; }
            set { _evt = value; }
        }

        public override void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
        {
            base._stressPeriods_StressPeriodChanged(sender, e);

            List<StressPeriod> periods = e.Param as List<StressPeriod>;

            this.EVT.OnResetSperiods(periods.Count);



        }

        #region 公共函数

        public void SetEVT(Evapotranspiration evt)
        {
            this.EVT.Option = evt.Option;
            int i = 0;
            foreach (EVTPeriod period in evt.Periods)
            {
                this.EVT.Periods[i].Multiplier = period.Multiplier;
                this.EVT.Periods[i].Elevation = period.Elevation;
                this.EVT.Periods[i].ExtinctDepth = period.ExtinctDepth;
                this.EVT.Periods[i].MaxETRate = period.MaxETRate;
                this.EVT.Periods[i].EvtLayer = period.EvtLayer;
                i++;
            }
            this.AskCollectionChanged(new SCColleconChangedEventArgs(evt, SCCollectionChangedType.MODIFY, SCType.EVT));
        }


        public PEvapotranspiration ToEVTPack()
        {
            PEvapotranspiration pe = new PEvapotranspiration();

            switch (this.EVT.Option)
            {
                case EVTOPTION.TOPLAYER: pe.NEVTOP = 1;
                    break;
                case EVTOPTION.VERTICALCELLS: pe.NEVTOP = 2;
                    break;
            }


            int k = 1;

            foreach (EVTPeriod item in this.EVT.Periods)
            {
                SPEVT spevt = new SPEVT();
                if (this.IsUsePrecious[k-1] == true)
                {

                    if (k == 1)
                    {

                        spevt.INSURF = 1;
                        spevt.INEVTR = 1;
                        spevt.INEXDP = 1;
                        spevt.SURF = item.Elevation;
                        spevt.EVTR = item.MaxETRate;
                        spevt.EXDP = item.ExtinctDepth;


                    }

                    else
                    {

                        spevt.INSURF = -1;
                        spevt.INEVTR = -1;
                        spevt.INEXDP = -1;
 

                    }


                 
                 
                }
                else
                {
                    spevt.INSURF = 1;
                    spevt.INEVTR = 1;
                    spevt.INEXDP = 1;
                    spevt.SURF = item.Elevation;
                    spevt.EVTR = item.MaxETRate;
                    spevt.EXDP = item.ExtinctDepth;
                }
                
               
                pe.StressPeriodEVT.Add(spevt);

                k++;



            }
            if (pe.NEVTOP == 1)
            {
                foreach (SPEVT item in pe.StressPeriodEVT)
                {
                    item.INIEVT = -1;
                }
            }
            else
            {
                int i=0;
                foreach (SPEVT item in pe.StressPeriodEVT)
                {
                    item.INIEVT = 1;
                    item.IEVT = EVT.Periods[i].EvtLayer;
                    i++;
                }
            }




            return pe;


        }


        public override void OnGridReset(int nrows, int ncols, int nlayers)
        {
            base.OnGridReset(nrows, ncols, nlayers);


            this._evt = new Evapotranspiration(nrows,ncols,nlayers,this.NPeriods);


        }


        #endregion

    }
}
